﻿using System;
using SmartHRMS.Infrastructure;
using StructureMap;

namespace SmartHRMS.Model.Account
{
    public class User : BusinessBase<User>, IAggregateRoot, IEntity
    {
        public Guid UserId { get; set; }

        public string UserName { get; set; }

        public string EmployeeCode { get; set; }

        public string Password { get; set; }

        public string RePassword { get; set; }

        public string LastPassword { get; set; }

        public CommonFields CommonFields { get; set; }

        public Mode Mode { get; set; }

        private IUserService _IUserService;

        public User()
        {
        }

        public User(IUserService _IUser)
        {
            this._IUserService = _IUser;
        }

        public override void validate()
        {
            if (string.IsNullOrEmpty(this.UserName))
            {
                this.BrokenRules.Add(new BrokenRule("UserName", "User name is a required value"));
            }
            else
            {
                if (this.UserName.Length > 50)
                {
                    this.BrokenRules.Add(new BrokenRule("UserName", "User name should be 50 chars or less"));
                }
                else
                {
                    if (this.Mode != Mode.Edit)
                    {
                        _IUserService = ObjectFactory.GetInstance<IUserService>();

                        if (_IUserService.IsUserNameExist(this.UserName) == true)
                        {
                            this.BrokenRules.Add(new BrokenRule("UserName", "User name aready exists please try other one."));
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(this.Password))
            {
                this.BrokenRules.Add(new BrokenRule("Password", "Password is a required value."));
            }
            else
            {
                if (this.Password.Length < 5 || this.Password.Length > 15)
                {
                    this.BrokenRules.Add(new BrokenRule("Password", "Password should be between 5 and 15 chars long."));
                }
            }

            if (string.IsNullOrEmpty(this.RePassword))
            {
                this.BrokenRules.Add(new BrokenRule("RePassword", "Re-Password is a required value."));
            }
            else
            {
                if (!this.RePassword.Equals(this.Password))
                {
                    this.BrokenRules.Add(new BrokenRule("RePassword", "Re-Password must be the same as the password."));
                }
            }

            if (string.IsNullOrEmpty(this.EmployeeCode))
            {
                this.BrokenRules.Add(new BrokenRule("EmployeeCode", "Employee code is a required value."));
            }
            else
            {
                if (this.EmployeeCode.Length > 50)
                {
                    this.BrokenRules.Add(new BrokenRule("EmployeeCode", "Employee code should be 50 chars or less."));
                }
            }
        }
    }
}
